import { cloneDeep } from 'lodash';
import { reactive, h, ref } from 'vue'
import { ColProps, FormItemProps, TableColumnsType, TableColumnProps, Space, Button } from "ant-design-vue";
import { _IFormEl } from "@/components/common/form/interface";
import dayjs from 'dayjs';
import { educations, sexs, techLevels, isSel1, nationalitys, policys } from '@/common/model/base'
import { useBaseStore } from '@/store'

const baseStore = useBaseStore()

const formatDate = (text: any): string => text ? dayjs(text).format('YYYY-MM-DD') : ''

// 格式枚举
export const formatEnum = (text: any, enums: any[], mode: string = 'single') => {
  if(mode != 'single') {
    let infos = enums.filter(i => (text || '').split(',').includes(i.value + ''));
    return infos.map(i => i.label).join(',')
  }
  let info = enums.find(i => i.value == text);
  return info ? info['label'] : ''
}

const renderWrapText = (text) => {
  return h("div", { style: { whiteSpace: 'pre-line' }}, text)
}

// 主列表数据
export const TColumns: TableColumnsType & any = reactive([
  { dataIndex: "departmentName", title: "所属部门", width: 150, resizable: true, },
  { dataIndex: "personnelName", title: "姓名", width: 100, align: 'center', resizable: true,  },
  { dataIndex: "gender", title: "性别", width: 80, align: 'center', resizable: true, 
    customRender: ({ text }) => formatEnum(text, baseStore.storesexs)
  },
  { dataIndex: "nation", title: "民族", width: 100, align: 'center', resizable: true,  },
  { dataIndex: "idCardNo", title: "身份证号码", width: 156, align: 'center', resizable: true,  },
  { dataIndex: "nativePlace", title: "籍贯", width: 140, resizable: true, },
  { dataIndex: "dateOfBirth", title: "出生年月", width: 120, align: 'center', resizable: true, 
    customRender: ({ text }) => formatDate(text)
  },
  { dataIndex: "employmentTime", title: "参加工作时间", width: 120, align: 'center', resizable: true, 
    customRender: ({ text }) => formatDate(text)
  },
  { dataIndex: "employmentYears", title: "到单位工作年月", width: 120, align: 'center', resizable: true, 
    customRender: ({ text }) => formatDate(text)
  },
  { dataIndex: "politicalStatus", title: "政治面貌", width: 120, align: 'center', resizable: true,  },
  { dataIndex: "groupTime", title: "入党时间", width: 100, align: 'center', resizable: true, 
    customRender: ({ text }) => formatDate(text)
  },
  { dataIndex: "keyp11", title: "学历", align: 'center',
    children: [
      { dataIndex: "degreeNameMax", title: "最高学历", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "degreeFormMax", title: "最高学历形式", width: 110, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "degreeCertificateMax", title: "最高学历证书", width: 110, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "professionTypeMax", title: "专业结构类型", width: 110, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "degreeNameInit", title: "初始学历", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "certificateInit", title: "初始学历取得的证书", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "graduationTimeInit", title: "初始学历毕业时间", width: 130, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "schoolInit", title: "初始学历毕业学校", width: 130, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "professionInit", title: "初始学历所学专业", width: 130, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "degreeNameJob", title: "在职学历", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "certificateJob", title: "在职学历取得证书", width: 140, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "graduationTimeJob", title: "在职学历毕业时间", width: 130, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "schoolJob", title: "在职学历毕业学校", width: 130, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "professionJob", title: "在职学历所学专业", width: 130, align: 'center', resizable: true,  },
    ]
  },
  { dataIndex: "keyp12", title: "专业技术职称", align: 'center',
    children: [
      { dataIndex: "technicalLevle", title: "级别", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "technicalProfession", title: "专业", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "tecApprovalDate", title: "批准日期", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "tecUnit", title: "证书颁发单位", width: 110, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "keyp112", title: "其他资格证书", align: 'center',
    children: [
      { dataIndex: "credentialName", title: "证书名称", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "credentialStats", title: "证书属性", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "creApprovalDate", title: "证书颁发日期", width: 110, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text) 
      },
      { dataIndex: "creUnit", title: "证书颁发单位", width: 110, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "creValidity", title: "证书有效期", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "job", title: "职务", width: 100, align: 'center', resizable: true, 
    customRender: ({ text }) => renderWrapText(text)
  },
  { dataIndex: "approvalDate", title: "职务批准时间", width: 100, align: 'center', resizable: true, 
    customRender: ({ text }) => renderWrapText(text) 
  },
  { dataIndex: "conUnit", title: "合同签定单位", width: 100, resizable: true, },
  { dataIndex: "conBeginDate1", title: "合同起止时间", width: 100, resizable: true,
    exportKey: {
      conBeginDate: '合同开始时间',
      conEndDate: '合同结束时间',
    }
  },
  { dataIndex: "perStatus", title: "人员状态", width: 100, align: 'center', resizable: true, 
    customRender: ({ text }) => {
      let label: { [key: string]: string } = { 0: '正式职工', 1: '离职', 2: '退休'}
      return label[text] || ''
    }
  },
  // { dataIndex: "key49", title: "转正时间", width: 100, align: 'center',
  //   customRender: ({ text }) => formatDate(text)  
  // },
  { dataIndex: "perRetireDate", title: "离/退休时间", width: 100, align: 'center', resizable: true, 
    customRender: ({ text }) => formatDate(text)  
  },
  { dataIndex: "perPhone", title: "本人联系电话", width: 100, align: 'center', resizable: true,  },
  { dataIndex: "perAge", title: "年龄", width: 100, align: 'center', resizable: true,  },
  { dataIndex: "companyAge", title: "司龄", width: 100, align: 'center', resizable: true,  },
  { dataIndex: "phoneNub1", title: "紧急联系人关系及电话", width: 100, resizable: true, 
    exportKey: {
      phoneName: '紧急联系人姓名',
      phoneNub: '紧急联系人电话',
      relation: '紧急联系人关系'
    },
    customRender: ({ text }) => renderWrapText(text)
  },
  { dataIndex: "domicileAddr", title: "户籍地址", width: 100, resizable: true, },
  { dataIndex: "keyp55", title: "受聘专家情况", align: 'center',
    children: [
      { dataIndex: "expertName", title: "专家名称", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "expertGroup", title: "专家组别", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "expertUnit", title: "聘用单位", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "expBeginDate1", title: "聘用起止时间", width: 130, resizable: true, 
        customRender: ({ text }) => renderWrapText(text),
        exportKey: {
          expBeginDate: '聘用开始时间',
          expEndDate: '聘用结束时间',
        },
      },
    ]
  },
  { dataIndex: "dispatchCause", title: "外派", width: 200, align: 'center', ellipsis: true, resizable: true,  },
  { dataIndex: "scientificResearchType", title: "科研情况", width: 100, align: 'center' , resizable: true, 
    customRender: ({ text }) => renderWrapText(text)
  },
  { dataIndex: "keyp61", title: "受表彰奖励情况", align: 'center',
    children: [
      { dataIndex: "commendDate", title: "时间", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text) 
      },
      { dataIndex: "commendUnit", title: "授予单位", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "commendName", title: "表彰奖励名称", width: 110, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "commendLevel", title: "表彰奖励级别", width: 110, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "keyp65", title: "受处分情况", align: 'center',
    children: [
      { dataIndex: "punishmentDate", title: "受处分时间", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "punUnit", title: "处分单位", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "punishmentName", title: "处分名称", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "influence", title: "影响情况", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "rests", title: "其他事项", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "keyp70", title: "年度考核情况", align: 'center',
    children: [
      { dataIndex: "annualAssessmentDate", title: "考核年度", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "annualAssessmentLevle", title: "等次", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "keyp73", title: "家庭主要成员及重要社会关系", align: 'center',
    children: [
      { dataIndex: "famRelation", title: "与本人关系", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
      { dataIndex: "famName", title: "姓名", width: 100, resizable: true, 
        customRender: ({ text }) => renderWrapText(text),
        exportKey: {
          famName: '家人姓名',
        },
      },
      { dataIndex: "famBirthDate", title: "出生年月", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text) ,
        exportKey: {
          famBirthDate: '家人出生年月',
        },
      },
      { dataIndex: "famPolitcalStatus", title: "政治面貌", width: 100, align: 'center', resizable: true, 
        customRender: ({ text }) => renderWrapText(text),
        exportKey: {
          famPolitcalStatus: '家人政治面貌',
        },
      },
      { dataIndex: "famUnit", title: "工作单位及职务", width: 130, resizable: true, 
        customRender: ({ text }) => renderWrapText(text)
      },
    ]
  },
  { dataIndex: "curriculumVitae", title: "简历", width: 100, ellipsis: true, resizable: true,  },
]);

// 主列表的更多筛选
const moreSearchItemProps: FormItemProps & _IFormEl = {
  labelWidth: '80px',
  wrapperCol: { span: 24 },
};
export const moreSearchEl: Array<_IFormEl> = [
  { label: "部门", key: "departmentId", element: "select", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      options: baseStore.deptData
    }
  },
  { label: "单位职务", key: "job", element: "input", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
  },
  { label: "人员状态", key: "perStatus", element: "select", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      options: [
        { label: '正式职工', value: 0},
        { label: '离职', value: 1},
        { label: '退休', value: 2},
      ]
    }
  },
  { label: "性别", key: "gender", element: "select", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      options: [
        { label: '女', value: 0},
        { label: '男', value: 1},
      ]
    }
  },
  { label: "最高学历", key: "highestDegree", element: "select", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      options: educations
    }
  },
  { label: "政治面貌", key: "politicalStatus", element: "select", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      options: policys
    }
  },
  { label: "司龄", key: "companyAge", element: "number", col: { span: 4 }, 
    formItemProps: moreSearchItemProps, 
    componentProps: {
      precision: 0, min: 1, controls: false
    }
  },
];

// 设置主列表可自定义选择的显示的列字段
let collapses = ref<any[]>([])
export const initSelEls = (): any[] => {
  collapses.value = []
  collapses.value.push(
    { 
      label: '基本信息', 
      activeKey: 'base', 
      els: TColumns.filter((i: any) => (i['children'] || []).length == 0),
    }
  )

  let childs = TColumns.filter((i: any) => (i['children'] || []).length != 0)
  childs.forEach((item: any) => {
    collapses.value.push({ 
      label: item.title, 
      activeKey: item.dataIndex, 
      els: (item['children'] || []),
    })
  })
  return collapses.value
}

const fRenderCols = ref<any[]>([]);
export const filterRenderCol = (): void => {
  let results: TableColumnsType[] = [];
  const filterC = (data: any[]) => {
    data.forEach((item: any) => {
      if(item['customRender']) {
        results.push(item)
      }
      if((item['children'] || []).length != 0) {
        filterC(item.children)
      }
    })
  }
  filterC(TColumns)

  fRenderCols.value = results;
  // return results;
} 
filterRenderCol()

export const equiRenderCol = (cols: any[]): any[] => {
  const renderCols = fRenderCols.value
  const equip = (data: any[]) => {
    data.forEach((item: any, index: number) => {
      let renderItem = renderCols.find(i => i.dataIndex == item.dataIndex);
      if(renderItem) {
        data.splice(index, 1, { ...item, ...renderItem })
      }
      if((item['children'] || []).length != 0) {
        equip(item['children'])
      }
    })
  }
  equip(cols)

  return cols
} 
